package com.xiaochong.tir.api.service.hc;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.mapper.ExchangeTransactionpairMiddleMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.util.RedisKeysUtils;

/**
 * 算立方
 * @author qzw
 * 2018年10月24日 下午2:32:37
 * @description 
 **/
@Service
public class HcService {

	@Autowired
	private CurrencyInfoMapper currencyInfoMapper;
	
	@Autowired
	private ExchangeTransactionpairMiddleMapper exchangeTransactionpairMiddleMapper;
	
	@Resource
    private RedisServer redisServer;
	
	/**
	 * 根据cmcId获取币价
	 * 返回示例：
	 * {
		  "code": "00",
		  "msg": "成功",
		  "data": {
		    "usdcny": 21.46,
		    "usdt": 3.09,
		    "en_name": "hsr"
		  }
		}
	 * @param cmcId
	 */
	public ApiHcResult<Map<String, String>> getCurrencyPrice(String cmcId){
		ApiHcResult<Map<String, String>> api = new ApiHcResult<Map<String,String>>();
		Map<String, String> m = new HashMap<String, String>();
		try{
			m.put("cmcId", cmcId);
			if(cmcId == null || "".equals(cmcId.trim())){
				returnError(api, m);
				return api;
			}
			
			//根据cmcId查询货币关联的exchangeTransactionpairMiddle的 id
			CurrencyInfo ci = new CurrencyInfo();
			ci.setCmcId(cmcId);
			CurrencyInfo c = currencyInfoMapper.selectByEntityOwn(ci);
			if(c != null && c.getExchangeTranMiddleId() != null){
				//根据货币关联的id查询ExchangeId（交易所id）和TransactionName（交易对名称）
				ExchangeTransactionpairMiddle t = exchangeTransactionpairMiddleMapper.selectByPrimaryKey(c.getExchangeTranMiddleId());
				if(t != null && t.getExchangeId() != null && t.getTransactionName() != null && !"".equals(t.getTransactionName())){
					//到缓存查询最新的价格
					String redisKey = RedisKeysUtils.API_SERVICE_K_ACTION_ALL_DATA_PRICE + "@" + t.getExchangeId() + "@" + t.getTransactionName();
					String result = redisServer.get(redisKey);
					if(result != null && !"".equals(result)){
						Map<String, String> map = JSON.parseObject(result, Map.class);
						returnSuccess(api, m, map);
					}else{
						returnFail(api, m);
					}
				}else{
					returnFail(api, m);
				}
			}else{
				returnFail(api, m);
			}
		}catch(Exception e){
			e.printStackTrace();
			returnFail(api, m);
		}
		return api;
	} 
	
	/**
	 * 失败返回
	 * @param api
	 * @param m
	 */
	public void returnFail(ApiHcResult<Map<String, String>> api, Map<String, String> m){
		api.setCode(ApiHcResult.Return.FAIL.returnCode);
		api.setMsg(ApiHcResult.Return.FAIL.returnMsg);
		m.put("usdcny", "0");
		m.put("usdt", "0");
		api.setData(m);
	}
	
	/**
	 * 参数错误返回
	 * @param api
	 * @param m
	 */
	public void returnError(ApiHcResult<Map<String, String>> api, Map<String, String> m){
		api.setCode(ApiHcResult.Return.ERROR.returnCode);
		api.setMsg(ApiHcResult.Return.ERROR.returnMsg);
		m.put("usdcny", "0");
		m.put("usdt", "0");
		api.setData(m);
	}
	
	/**
	 * 成功返回
	 * @param api
	 * @param m
	 */
	public void returnSuccess(ApiHcResult<Map<String, String>> api, Map<String, String> m, Map<String, String> map){
		api.setCode(ApiHcResult.Return.SUCCESS.returnCode);
		api.setMsg(ApiHcResult.Return.SUCCESS.returnMsg);
		String usdPrice = map.get("lastOpenPrice");
		String cnyPrice = map.get("openPriceCny");
		m.put("usdcny", cnyPrice);
		m.put("usdt", usdPrice);
		api.setData(m);
	}
}
